package wiki.hitime.stl.daq.service.admin;

import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import wiki.hitime.stl.daq.bean.base.NameValue;
import wiki.hitime.stl.daq.bean.dto.manage.*;
import wiki.hitime.stl.daq.bean.dto.req.*;
import wiki.hitime.stl.daq.enums.ExecuteActionEnum;
import wiki.hitime.stl.daq.exception.ServiceException;
import wiki.hitime.stl.daq.manager.DaqAcquisitionRuleManager;
import wiki.hitime.stl.daq.manager.DaqCheckRuleManager;
import wiki.hitime.stl.daq.manager.DaqExecuteRuleManager;
import wiki.hitime.stl.daq.manager.DaqMsgRuleManager;

import java.text.ParseException;
import java.util.List;

/**
 * 任务管理
 *
 * @author zhangdaoping
 * @create 2019-07-23 14:41
 */
@Service
public class RuleManageService {

    private DaqExecuteRuleManager daqExecuteRuleManager;
    private DaqAcquisitionRuleManager daqAcquisitionRuleManager;
    private DaqCheckRuleManager daqCheckRuleManager;
    private DaqMsgRuleManager daqMsgRuleManager;

    public RuleManageService(DaqExecuteRuleManager daqExecuteRuleManager,
                             DaqAcquisitionRuleManager daqAcquisitionRuleManager,
                             DaqCheckRuleManager daqCheckRuleManager,
                             DaqMsgRuleManager daqMsgRuleManager) {
        this.daqExecuteRuleManager = daqExecuteRuleManager;
        this.daqAcquisitionRuleManager = daqAcquisitionRuleManager;
        this.daqCheckRuleManager = daqCheckRuleManager;
        this.daqMsgRuleManager = daqMsgRuleManager;
    }

    public ExecuteRuleViewPageDTO listExecuteRuleView(ExecuteRuleViewListReqDTO reqDTO) {
        ExecuteRuleViewPageDTO executeRuleViewPageDTO = new ExecuteRuleViewPageDTO();
        PageInfo pageInfo = daqExecuteRuleManager.listRuleView(reqDTO);
        List<NameValue> executeActionList = ExecuteActionEnum.getList();
        executeRuleViewPageDTO.setPageInfo(pageInfo);
        executeRuleViewPageDTO.setExecuteActionList(executeActionList);
        return executeRuleViewPageDTO;
    }

    /**
     * 获取执行规则
     *
     * @param reqDTO 请求实体
     * @return
     */
    public ExecuteRuleDTO getExecuteRule(ExecuteRuleGetReqDTO reqDTO) {
        return daqExecuteRuleManager.getDTO(reqDTO);
    }

    /**
     * 获取采集规则
     *
     * @param reqDTO 请求实体
     * @return
     */
    public AcquisitionRuleDTO getAcquisitionRule(AcquisitionRuleGetReqDTO reqDTO) {
        return daqAcquisitionRuleManager.getDTO(reqDTO);
    }

    /**
     * 获取检查规则
     *
     * @param reqDTO 请求实体
     * @return
     */
    public CheckRuleDTO getCheckRule(CheckRuleGetReqDTO reqDTO) {
        return daqCheckRuleManager.getDTO(reqDTO);
    }

    /**
     * 获取采集规则集合
     *
     * @param reqDTO 请求实体
     * @return
     */
    public List<CheckRuleDTO> listCheckRule(CheckRuleListReqDTO reqDTO) {
        return daqCheckRuleManager.listDTO(reqDTO);
    }

    /**
     * 获取消息规则
     *
     * @param reqDTO 请求实体
     * @return
     */
    public MsgRuleDTO getMsgRule(MsgRuleGetReqDTO reqDTO) {
        return daqMsgRuleManager.getDTO(reqDTO);
    }

    /**
     * 添加任务
     *
     * @param executeTaskAddReqDTO 任务实体
     * @throws ParseException
     */
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void addTask(ExecuteTaskAddReqDTO executeTaskAddReqDTO) throws ServiceException {
        // 保存执行规则
        Long executeRuleId = daqExecuteRuleManager.add(executeTaskAddReqDTO.getExecuteRuleDTO());
        // 保存数据采集规则
        AcquisitionRuleDTO acquisitionRuleDTO = executeTaskAddReqDTO.getAcquisitionRuleDTO();
        acquisitionRuleDTO.setExecuteRuleId(executeRuleId);
        Long acquisitionRuleId = daqAcquisitionRuleManager.add(acquisitionRuleDTO);
        // 保存数据检查规则和消息通知规则
        daqCheckRuleManager.addList(executeRuleId, acquisitionRuleId, executeTaskAddReqDTO.getCheckRuleDTOList());
    }

    /**
     * 修改执行规则
     *
     * @param reqDTO 请求实体
     * @return
     * @throws ServiceException
     */
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public Boolean updateExecuteRule(ExecuteRuleUpdateReqDTO reqDTO) throws ServiceException {
        return daqExecuteRuleManager.updateRule(reqDTO);
    }

    /**
     * 修改采集规则
     *
     * @param reqDTO 请求实体
     * @return
     * @throws ServiceException
     */
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public Boolean updateAcquisitionRule(AcquisitionRuleUpdateReqDTO reqDTO) throws ServiceException {
        return daqAcquisitionRuleManager.updateRule(reqDTO);
    }

    /**
     * 修改检查规则
     *
     * @param reqDTO 请求实体
     * @return
     * @throws ServiceException
     */
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public Boolean updateCheckRule(CheckRuleListAddUpdateReqDTO reqDTO) throws ServiceException {
        return daqCheckRuleManager.updateRule(reqDTO);
    }

    /**
     * 修改消息规则
     *
     * @param reqDTO 请求实体
     * @return
     * @throws ServiceException
     */
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public Boolean updateMsgRule(MsgRuleUpdateReqDTO reqDTO) throws ServiceException {
        return daqMsgRuleManager.updateRule(reqDTO);
    }

    /**
     * 修改执行状态
     *
     * @param reqDTO
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public Boolean updateExecuteAction(ExecuteRuleUpdateActionReqDTO reqDTO) {
        return daqExecuteRuleManager.updateExecuteAction(reqDTO);
    }

    /**
     * 删除执行任务
     *
     * @param reqDTO
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public Boolean deleteTask(ExecuteRuleDeleteReqDTO reqDTO) {
        // 删除执行规则
        daqExecuteRuleManager.delete(reqDTO.getExecuteRuleId());
        // 删除数据采集规则
        daqAcquisitionRuleManager.delete(reqDTO.getExecuteRuleId());
        // 删除数据检查规则和消息通知规则
        daqCheckRuleManager.delete(reqDTO.getExecuteRuleId());
        return Boolean.TRUE;
    }
}
